$.fn.extend({
    Verification: function(options) {
        var T = this
        var settings = {
            vali_btn: ".vali_btn",
            ignoreHidden: false,
            ajaxpost: true,
            showAllError: true,
            onError: function(o, txt) {},
            onSuccess: null,
            onStartFun: null,
            onCallback: null,
			onFirstFun: null,
			onType:0,
            beforeCheck: function(e) {
                return true },
            beforeSubmit: function() {},
            callback: function() {}
        }
        $.extend(settings, options);
        this.each(function(index, element) {
            vali = {
                "match": /^(.+?)(\d+)-(\d+)$/,
                "z": /[\w\W]+/,
                "*6-16": /^[\w\W]{6,16}$/,
                "n": /^\d+$/,
                "pw":/^[\w]{6,25}$/,
                "n6-16": /^\d{6,16}$/,
                "s": /^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]+$/,
                "s6-18": /^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]{6,18}$/,
                "p": /^[0-9]{6}$/,
                "m": /^13[0-9]{9}$|14[0-9]{9}$|15[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$/,
				"tel" :/((^(0\d{2})-(\d{8})$)|(^(0\d{3})-(\d{7,8})$)|(^(0\d{2})-(\d{8})-(\d+)$)|(^(0\d{3})-(\d{7,8})-(\d+)$)|(^(0\d{2})(\d{8})$)|(^(0\d{3})(\d{7,8})$)|(^(0\d{2})(\d{8})-(\d+)$)|(^(0\d{3})(\d{7,8})-(\d+)$))|^13[0-9]{9}$|14[0-9]{9}$|15[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$/,
                "e": /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
                "xz": /(^[1-9]\d{0,1}$)|(^[1-9]\d{1}[0]$)/,
                "url": /(http:\/\/|ftp:\/\/|https:\/\/|www)?[^\u4e00-\u9fa5\s\uFE30-\uFFA0]*?\.([a-zA-Z0-9]|[1-255])[^\u4e00-\u9fa5\s\uFE30-\uFFA0]*/
            }
            function CheckAll(o) {
                Result = true;
                T.find(".Rcheck").find("textarea,select,input:not('.nobind')").each(function(i, e) {
                    if (typeof(o) != "undefined") {
                        var T = o
                    } else {
                        var T = $(this);
                    }
                    var datatype = T.attr("datatype");
                    var _v = T.val();
                    var errormsg = T.attr("errormsg")
                    var nullmsg = T.attr("nullmsg");
                    var recheck = T.attr("recheck");
                    var jmz = {};
					var type=settings.onType
					jmz.GetLength = function(str) {
						var realLength = 0, len =str.length, charCode = -1;
						for (var i = 0; i < len; i++) {
							charCode = str.charCodeAt(i);
							if(type==1){
									if (charCode >= 0 && charCode <= 128) realLength += 0.5;
									    else 
									{ 
									  realLength += 1;
									}
							}
							else
							{
								  if (charCode >= 0 && charCode <= 128) realLength += 1;
									else 
									{ 
									  realLength += 1;
									}
							}
						}
						return Math.round(realLength);
					 }
                    $.fn.setCursorPosition = function(position) {
                        if (this.lengh == 0) return this;
                        return $(this).setSelection(position, position);
                    }
                    $.fn.setSelection = function(selectionStart, selectionEnd) {
                        if (this.lengh == 0) return this;
                        input = this[0];
                        if (input.createTextRange) {
                            var range = input.createTextRange();
                            range.collapse(true);
                            range.moveEnd('character', selectionEnd);
                            range.moveStart('character', selectionStart);
                            range.select();
                        } else if (input.setSelectionRange) {
                            input.focus();
                            input.setSelectionRange(selectionStart, selectionEnd);
                        }
                        return this;
                    }
                    $.fn.focusEnd = function() {
                        this.setCursorPosition(this.val().length);
                    }

                    if (!datatype) {
                        if (T.attr("length")) {
                            var length = eval(T.attr("length"));
                            var m_min = length[0];
                            var m_max = length[1];
                            var shu = jmz.GetLength(T.val());
                            if (m_min <= shu && shu <= m_max) {
                                flag = true;
                                settings.onSuccess(T)
                            } 
							else 
						   {
                                if (shu == 0) {
                                    settings.onError(T, nullmsg)
                                } else {
                                    settings.onError(T, errormsg)
                                }
                                Result = false
                            }
                            if (T.attr("AddFun")) {
                                settings.onCallback(T)
                            }

                        }
                        if ($("[recheck]").length > 0) { //2个字段进行对比
                            if ($("[recheck=" + recheck + "]").val() == $("[name=" + recheck + "]").val()) {
                                Result = true;
                                settings.onSuccess(T)
                            } else {
                                if (_v == "") {
                                    settings.onError($("[recheck=" + recheck + "]"), $("[recheck=" + recheck + "]").attr("nullmsg"))

                                } else {
                                    settings.onError($("[recheck=" + recheck + "]"), $("[recheck=" + recheck + "]").attr("errormsg"))
                                }
                                Result = false
                            }
                        }
                    } else {
                        if (typeof(datatype) != "undefined" && datatype.indexOf("/") == 0) { //如果类型为正则表达式
                            if (_v == "") {
                                settings.onError(T, nullmsg)
                                Result = false
                            } else {
                                if (!eval(datatype).test(_v)) {
                                    settings.onError(T, errormsg)
                                    Result = false
                                } else {
                                    settings.onSuccess(T)
                                    if (T.attr("AddFun")) {
                                        settings.onCallback(T)
                                    }
                                }
                            }
                        } else //否则默认定义好的类型
                        {
                            if (T.attr("type") == "hidden" && typeof(datatype) == "undefined") { //当类型是隐藏域并且没有指定验证类型
                                if (settings.ignoreHidden) {
                                    settings.onError(T, nullmsg)
                                    Result = false
                                } else {
                                    settings.onSuccess(T)
                                }
                            } else {
                                if (_v == "" || _v == "请选择") {
                                    settings.onError(T, nullmsg)
                                    Result = false
                                } else {
                                    if (T.attr("type") == "radio" || T.attr("type") == "checkbox") {
                                        var val = $('input:' + T.attr("type") + '[name=' + T.attr("name") + ']:checked').val();
                                        if (val == null) {
                                            settings.onError(T, errormsg)
                                            Result = false
                                        } else {
                                            settings.onSuccess(T)
                                        }


                                    } else {
                                        if (typeof(datatype) != "undefined") {
                                            if (!eval("vali." + datatype + ".test(_v)")) {
                                                settings.onError(T, errormsg)
                                                Result = false
                                            } else {
                                                settings.onSuccess(T)
                                                if (T.attr("AddFun")) {
                                                    settings.onCallback(T)
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (!settings.showAllError) {
                        return Result
                    }
                });
                return Result
            }
			//处理必填 isInput nobind 两个class
			T.find(".Rcheck").find(".nobind").each(function(i, e) {
				 var T=$(this)
				 T.on("input",function(){
					   if($(this).val().length>0){
						   $(this).removeClass("nobind")
				       }
					   else
					   {
						   $(this).addClass("nobind");
						}
				  })
			 })
			T.find("[FirstFun]").each(function(index, element) {
                var T = $(this);
                    T.click(function(e) {
                        settings.onFirstFun(T)
                    });
					if (T.val() != "") {
                         settings.onFirstFun(T)
                    }
             });
            T.find(".Rcheck").find("textarea,select,input:not('.nobind')").each(function(i, e) {
                    var T = $(this);
					if($(this).attr("datatype")!="z"){
						T.blur(function(e) {
							CheckAll(T);
							e.preventDefault();
						})
					}
                    /*T.blur(function(e) {
                        CheckAll(T);
                        e.preventDefault();
                    })
                    T.focus(function(e) {
                        if (!settings.showAllError) {
                            CheckAll(T);
                        }
                        e.preventDefault();
                    });
                    T.click(function(e) {
                        if (!settings.showAllError) {
                            CheckAll(T);
                        }
                        e.preventDefault();
                    });
					*/
                    T.bind("checkall", function() {
                        CheckAll(T);
                    });
                })
                //AddFun初始化处理
            T.find("[AddFun]").each(function(index, element) {
                var T = $(this);
				if(T.hasClass("isInput")){
					T.on("input",function(e){
						 CheckAll(T);
					})
				}
            });
		     $(settings.vali_btn).click(function(e) {
                if (typeof settings.beforeCheck=="function"&&!settings.beforeCheck(e)) {
                    return false
                }
                Result = CheckAll()
                $(".Rcheck").find(".kz_error:not('.nofocus')").eq(0).focus()
                if(Result){
                    if (!settings.ajaxpost) {
                        if (!settings.beforeSubmit(e)) {
                            Result = false
                        } else {
                            $("form").submit();
                            Result = true
                        }
                    } else {
                        settings.beforeSubmit(e)
                    }
                }
                return Result
            })

        });
    }

})
